package com.wonders.frame.autoGeneration.task;

import com.wonders.frame.autoGeneration.support.TaskContent;
import com.wonders.frame.autoGeneration.task.Core.Task;
import com.wonders.frame.common.util.FileUtil;
import com.wonders.frame.common.util.HumpUtil;

import java.io.File;

/**
 * @author 顾乘瑞
 */
public class GenRepositoryTask extends Task {
    private static final String PATH = System.getProperty("file.separator") +"src"
            + System.getProperty("file.separator") + "main"
            + System.getProperty("file.separator") + "java"
            + System.getProperty("file.separator") + "com"
            + System.getProperty("file.separator") + "wonders"
            + System.getProperty("file.separator") + "frame"
            + System.getProperty("file.separator") + "repository"
            + System.getProperty("file.separator");

    public GenRepositoryTask(final TaskContent cont) {
        super(cont);
    }

    @Override
    public Object call(){
        String tableName = cont.getTableName();
        String entityClassName = HumpUtil.tableName2ClassName(tableName);
        String className = entityClassName + "Repository";

        String filePath = TaskContent.PROJECT_PATH + PATH;
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        String writeFilePath = filePath + "/" + className + ".java";
        StringBuilder content = new StringBuilder("package com.wonders.frame.repository;\n" +
                "\n" +
                "import com.wonders.frame.entity." + entityClassName + ";\n" +
                "import com.wonders.frame.repository.core.CoreRepository;\n" +
                "import org.springframework.stereotype.Repository;\n" +
                "\n" +
                "@Repository\n" +
                "public interface " + className + " extends CoreRepository<" + entityClassName + "> {\n" +
                "}");
        FileUtil.createFile(content.toString(), writeFilePath);
        return null;
    }
}
